<?php
if (!defined("LEVEL") || !defined("OK")) { header("Location: ?"); exit; }
if (isset($url['m']) && isnum($url['m']) && $url['m'] > 0) { $mid = (int)$url['m']; }
else { $mid = 0; }

// ############ Apdorojomi duomenys kurie buvo pateikti is tam tikros redagavimo lenteles #####################
// ######### Slaptazodzio keitimas #############
if (isset($_POST['action']) && $_POST['action'] == 'Keisti slaptažodį') {
	$old_pass = md5($_POST['old_pass']);
	$sql = mysql_num_rows(mysql_query1("SELECT * FROM `".LENTELES_PRIESAGA."users` WHERE nick=".escape($_SESSION['username'])." AND pass=".escape($old_pass).""));
	if ($sql != 0) {
		$new_pass = md5($_POST['new_pass']);
		$new_pass2 = md5($_POST['new_pass2']);
		if ($new_pass == $new_pass2) {
			mysql_query1("UPDATE `".LENTELES_PRIESAGA."users` SET pass=".escape($new_pass)." WHERE nick=".escape($_SESSION['username'])."");
			msg("Info","Slaptažodis sėkmingai pakeistas");
			//echo LEVEL;
			//echo $_SESSION['username'];
		}
		else {
			klaida("Klaida","Nesutampa naujieji slaptažodžiai");
		}
	}
	else {
		klaida("Klaida","Klaidingas senasis slaptažodis");
	}
	unset($old_pass,$sql,$new_pass,$new_pass2);
}
// ################# kontaktu keitimas ######################
if (isset($_POST['action']) && $_POST['action'] == 'contacts_change') {
	$icq = input($_POST['icq']);
	$msn = input($_POST['msn']);
	$skype = input($_POST['skype']);
	$yahoo = input($_POST['yahoo']);
	$aim = input($_POST['aim']);
	$url = input($_POST['url']);
	$email = input($_POST['email']);
	mysql_query1("UPDATE `".LENTELES_PRIESAGA."users` SET icq=".escape($icq) .", msn=".escape($msn).", skype=".escape($skype).", yahoo=".escape($yahoo).", aim=".escape($aim).", url=".escape($url).", email=".escape($email)." WHERE nick=".escape($_SESSION['username'])."");
	msg("Info","Kontaktiniai duomenys sėkmingai atnaujinti");
	unset($icq,$msn,$skype,$yahoo,$aim,$url,$email);
}
// ################ Salies bei miesto nustatymai #############
if (isset($_POST['action']) && $_POST['action'] == 'country_change') {
	$miestas = input($_POST['miestas']);
	$salis = input($_POST['salis']);
	mysql_query1("UPDATE `".LENTELES_PRIESAGA."users` SET salis=" . escape($salis) .", miestas=" . escape($miestas) ." WHERE nick=" . escape($_SESSION['username']) ."");
	msg("Info","Vietovės duomenys atnaujinti sėkmingai");
}

// ################ Keiciu Avatara ######################################
if (isset($_POST['avatar']) && $_POST['avatar'] != '' && file_exists($_POST['avatar'])) {
	mysql_query1("UPDATE `".LENTELES_PRIESAGA."users` SET avatar=".escape($_POST['avatar'])." WHERE nick=" . escape($_SESSION['username']) ."");
	msg("Info","Avataras pakeistas sėkmingai");
}
// ################ Avataro keitimas #####################################
if (isset($_POST['action']) && $_POST['action'] == 'image_change') {
	$failai = show_files("./images/avatars/".$_POST['dir'],"jpg|png|gif");
	$content = '<form method="post">';
	if (isset($failai)) {
		foreach ($failai as $key => $row) {
			if (isset($row['tipas'])) { $content .= "<input type=\"image\" name=\"avatar\" value=\"".$row['failas']."\" src=\"".$row['failas']."\" title=\"".$row['vardas']."\" />\n"; }
		}
	}
	else { $content = "Tusčcia"; }
	$avatar_text = $content."</form>";
}
// ################ Parodoma Avataro direktorijos BET nerodomi avatarai #############
if (isset($_POST['action']) && $_POST['action'] == 'avatar_change') {
	$avatar_text = "<fieldset>
  			<legend>Direktorijos pasirinkimas:</legend>
  			<form name=\"avatar_change\" action=\"\" method=\"post\">
  			<table border=0 width=100%>
  				<tr>
  					<td align='left' width='10%'>Kategorija:</td>
  					<td><select size=\"1\" name=\"dir\">";
	$tdir = "./images/avatars";
	$dir = opendir("./images/avatars");
	while ($dira = readdir($dir)) {
		if (is_dir($tdir."/".$dira) && $dira != "." && $dira != "..") {
			$avatar_text .= "<option value='" . $dira ."'>$dira</option>\n";
		}
	}
	$avatar_text .= "</select>
					</td>
				</tr>
			</table>
			<input type=\"submit\"  value=\"Pasirinkti\">
			<input type=\"hidden\" name=\"action\"  value=\"image_change\" />
			</form>
			</fieldset>
	";
}

// ################ Pagrindiniu nustatymu keitimas ###################
if (isset($_POST['action']) && $_POST['action'] == 'default_change') {
	$vardas = input($_POST['vardas']);
	$pavarde = input($_POST['pavarde']);
	$metai = (int)$_POST['metai'];
	$menesis = (int)$_POST['menesis'];
	$diena = (int)$_POST['diena'];
	$parasas = input($_POST['parasas']);
	$gimimas = $metai . "-" . $menesis ."-" . $diena;
	mysql_query1("UPDATE `".LENTELES_PRIESAGA."users` SET vardas='" . $vardas . "', pavarde='" . $pavarde ."', parasas='" . $parasas ."', gim_data='" . $gimimas ."' WHERE nick='" . $_SESSION['username'] ."'");
	msg("Info","Pagrindiniai duomenys sėkmingai pakeisti");
}
// ################ Siulomi punktai redagavimui MENIU ##########################
$text = "
 <table width=100% border=0>
	<tr>
		<td>
			<div class=\"blokas\"><center><a href='?id,44;m,1'><img src=\"images/user/user-auth.png\" alt=\"slaptazodis\" />Slaptažodis</a></center></div>
			<div class=\"blokas\"><center><a href='?id,44;m,2'><img src=\"images/user/user-contact.png\" alt=\"kontaktai\" />Kontaktai</a></center></div>
			<div class=\"blokas\"><center><a href='?id,44;m,3'><img src=\"images/user/user-place.png\" alt=\"vietove\" />Vietovė</a></center></div>
			<div class=\"blokas\"><center><a href='?id,44;m,4'><img src=\"images/user/user-avatar.png\" alt=\"avataras\" />Avataras</a></center></div>
			<div class=\"blokas\"><center><a href='?id,44;m,5'><img src=\"images/user/user-settings.png\" alt=\"nustatymai\" />Apie save</a></center></div>
			<div class=\"blokas\"><center><a href='http://mightmedia.googlecode.com/svn/wiki/edit_user.wiki' target='_blank'><img src=\"images/user/help.png\" alt=\"Pagalba\" />Pagalba</a></center></div>
		</td>
	</tr>
</table>
";

// ######################### Jei pasirinktas vienas is pasiulytu MENIU ####################
if (isset($mid) && isnum($mid)) {
	// Pakeisti slaptazodi
	if ($mid == 1) {
		include_once("priedai/class.php");
		$form = array(
			"Form"=>array("action"=>"","method"=>"post","enctype"=>"","id"=>"","class"=>"","name"=>"change_password"),
			"Senas slaptažodis:"=>array("type"=>"password","value"=>"","name"=>"old_pass","style"=>"width:200px"),
			"Naujas slaptažodis:"=>array("type"=>"password","value"=>"","name"=>"new_pass","style"=>"width:200px"),
			"Pakartokite slaptažodį:"=>array("type"=>"password","value"=>"","name"=>"new_pass2","style"=>"width:200px"),
			""=>array("type"=>"submit","name"=>"action","value"=>"Keisti slaptažodį")
		);
		$bla = new forma();
		$text .= $bla->form($form,"Slaptažodžio keitimas");
	}
	// Pakeisti kontaktinius duomenis
	if ($mid == 2) {
		$info = mysql_fetch_assoc(mysql_query1("SELECT * FROM `".LENTELES_PRIESAGA."users` WHERE nick=" . escape($_SESSION['username']) .""));
		$text .= "
				<fieldset>
					<legend>Kontaktiniai duomenys</legend>
					<form name='change_contacts' action='' method='post' onSubmit=\"return checkMail('change_contacts','email')\">
					<table border=0 width=100%>
						<tr>
							<td align='right' width='15%'>ICQ:</td>
							<td><input name=\"icq\" type=\"text\" value=" . input($info['icq']) ."></td>
						</tr>
						<tr>
							<td align='right'>MSN:</td>
							<td><input name=\"msn\" type=\"text\" value=" . input($info['msn']) ."></td>
						</tr>
						<tr>
							<td align='right'>Skype:</td>
							<td><input name=\"skype\" type=\"text\" value=" . input($info['skype']) ."></td>
						</tr>
						<tr>
							<td align='right'>Yahoo:</td>
							<td><input name=\"yahoo\" type=\"text\" value=" . input($info['yahoo']) ."></td>
						</tr>
						<tr>
							<td align='right'>AIM:</td>
							<td><input name=\"aim\" type=\"text\" value=" . input($info['aim']) ."></td>
						</tr>
						<tr>
							<td align='right'>Iterneto svetainė:</td>
							<td><input name=\"url\" type=\"text\" value=" . input($info['url']) ."></td>
						</tr>
						<tr>
							<td align='right'>El. Paštas:</td>
							<td><input name=\"email\" type=\"text\" value=" . input($info['email']) ."></td>
						</tr>
						<tr>
							<td colspan=2>
								<input type=\"submit\" value=\"Atnaujinti\">
								<input type=\"hidden\" name=\"action\" value=\"contacts_change\" />
							</td>
						</tr>
					</table>
					</form>
				</fieldset>
			";
		unset($info);
	}
	// Pakeisti sali, miesta
	if ($mid == 3) {
		$info = mysql_fetch_assoc(mysql_query1("SELECT salis, miestas FROM `".LENTELES_PRIESAGA."users` WHERE nick='" . $_SESSION['username'] ."'"));
		$text .= "
				<fieldset>
					<legend>Vietovės nustatymas</legend>
					<form name='change_country' action='' method='post'>
					<table border=0 width=100%>
						<tr>
							<td align='right' width='5%'>Šalis:</td>
							<td>
								<select size=\"1\" name=\"salis\">
		";
		$sql = mysql_query1("SELECT * FROM `".LENTELES_PRIESAGA."salis`");
		while ($row = mysql_fetch_assoc($sql)) {
			$text .= "<option value='" . $row['iso'] ."' ";
			if ($row['iso'] == $info['salis']) { $text .= "selected"; }
			$text .=  ">" . $row['printable_name'] . "</option>\n";
		}
		$text .= "		</select>
  					</td>
  				</tr>
  				<tr>
  					<td align='right'>Miestas:</td>
  					<td><input name=\"miestas\" type=\"text\" value=" . $info['miestas'] .">
  				</tr>
  				<tr>
					<td colspan=2>
						<input type=\"submit\" value=\"Atnaujinti\">
						<input type=\"hidden\" name=\"action\" value=\"country_change\" />
					</td>
				</tr>
  			</table>
  			</form>
  			</fieldset>
  		";
		unset($info,$sql,$row);
	}

	// Avataro keitimas
	if ($mid == 4) {
		$sql = mysql_fetch_assoc(mysql_query1("SELECT `avatar` FROM `".LENTELES_PRIESAGA."users` WHERE `nick`='" . $_SESSION['username'] ."'"));
		$vimg = "<img src='" . $sql['avatar'] ."'></img>";
		$text .= "
  			<fieldset>
  			<legend>Paveiksliuko pasirinkimas:</legend>
  			<center>
			<h3>".basename($sql['avatar'])."</h3>
  			".$vimg."
  			<br/><form name=\"avatar_change\" action=\"\" method=\"post\">
			<input type=\"submit\" value=\"Pasikeisti\">
			<input type=\"hidden\" name=\"action\"  value=\"avatar_change\" />
			</form>
			".(isset($avatar_text)?$avatar_text:'')."
			</center>
			</fieldset>
		";
	}
	// Pagrindiniai nustatymai
	if ($mid == 5) {
		$sql = mysql_fetch_assoc(mysql_query1("SELECT * FROM `".LENTELES_PRIESAGA."users` WHERE nick='" . $_SESSION['username'] ."'"));
		$data = explode("-",$sql['gim_data']);
		$text .= "
  			<fieldset>
  			<legend>Pagrindiniai nustatymai</legend>
  			<form name=\"pagr_nustatymai\" action=\"\" method=\"post\">
  			<table border=0 width=100%>
  				<tr>
  					<td align='right' width='15%'>Vardas:</td>
  					<td><input name=\"vardas\" type=\"text\" value=" . input($sql['vardas']) ."></td>
  				</tr>
  				<tr>
  					<td align='right'>Pavardė:</td>
  					<td><input name='pavarde' type=\"text\" value=" . input($sql['pavarde']) ."></td>
  				</tr>
  				<tr>
  					<td align='right'>Gimimo data:</td>
  					<td>
  					<select size=\"1\" name=\"diena\">";
		$a = 1;
		while ($a < 31) {
			$text .= "<option value=" . $a . " ";
			if($a == $data[2]) { $text .= "selected"; }
			$text .= " >$a</option>\n";
			$a++;
		}
		unset($a);
		$text .= "
  					</select>
  					<select size=\"1\" name=\"menesis\">
  						<option value=\"1\" "; if($data[1] == 1) { $text .= "selected"; } $text .=">Sausis</option>
  						<option value=\"2\" "; if($data[1] == 2) { $text .= "selected"; } $text .=">Vasaris</option>
  						<option value=\"3\" "; if($data[1] == 3) { $text .= "selected"; } $text .=">Kovas</option>
  						<option value=\"4\" "; if($data[1] == 4) { $text .= "selected"; } $text .=">Balandis</option>
  						<option value=\"5\" "; if($data[1] == 5) { $text .= "selected"; } $text .=">Gegužė</option>
  						<option value=\"6\" "; if($data[1] == 6) { $text .= "selected"; } $text .=">Birželis</option>
  						<option value=\"7\" "; if($data[1] == 7) { $text .= "selected"; } $text .=">Liepa</option>
  						<option value=\"8\" "; if($data[1] == 8) { $text .= "selected"; } $text .=">Rugpjūtis</option>
  						<option value=\"9\" "; if($data[1] == 9) { $text .= "selected"; } $text .=">Rugsėjis</option>
  						<option value=\"10\" "; if($data[1] == 10) { $text .= "selected"; } $text .=">Spalis</option>
  						<option value=\"11\" "; if($data[1] == 11) { $text .= "selected"; } $text .=">Lapkritis</option>
  						<option value=\"12\" "; if($data[1] == 12) { $text .= "selected"; } $text .=">Gruodis</option>
					</select>
					<select size=\"1\" name=\"metai\">";
		$a = date("Y") - 80;
		$viso = date("Y") - 10;
		while ($a < $viso) {
			$text .= "<option value=" . $a ." ";
			if ($data[0] == $a) { $text .= "selected"; } $text .=">$a</option>\n";
			$a++;
		}
		unset($viso,$a);
		$text .="</select></td>
				</tr>
				<tr>
					<td valign='top' align='right'>Parašas:</td>
					<td><textarea name=\"parasas\" rows=5 cols=30 wrap=\"on\">" . input($sql['parasas']) ."</textarea></td>
				</tr>
			</table>
			<input type=\"submit\" value=\"Atnaujinti\">
			<input type=\"hidden\" name=\"action\" value=\"default_change\" />
			</form>
			</fieldset>";
	}
}
// ############## VARTOTOJO Informacija ##############
else {
	include "puslapiai/view_user.php";
	$text .= "</td></tr></table>";
}
lentele("Vartotojo nustatymai",$text);
?>
<script language="JavaScript1.2">
function checkMail(form,email) {
	var x = document.forms[form].email.value;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) { return true; }
	else { alert('Prašome patikrinti ar teisingai įvedėte e-pašto adresą'); return false; }
}
</script>
<?
function show_files($dir,$typ) {
	//$dir = "./images/gif";                                     //direktorija pvz: "gallerija"
	//==============[Pradzia]===============
	// Autorius: FDisk
	// Svetaine: www.mrcbug.com
	// Emailas: projektas[eta]gmail[taskas]com
	// Data: 2005.08.29
	// Apie: Sudeda failu esanc(iu; nurodytoje direktorijoje informacija; i; masyva; $failai
	$failai = array();                              //"$failai" kintamaji verciam i masyva
	if (is_dir($dir)) { $d = opendir($dir); }       //jeigu nurodyta direktorija egzistuoja
	else {                                          //kitu atveju metam klaidos pranesima
		user_error("<b><font color=red>Tokio folderio nera</font></b>");
		exit;
	}
	while ($failas = readdir($d)) {                 //Paleidziame cikla
		if (is_file($dir.'/'.$failas)) {            //Jeigu tai failas tesiam veiksma
			$a = explode(".",basename($failas));      //suskaldom failo pavadinima pagal taskus
			$ext = $a[count($a) - 1];                 //nustatom failo tipa
			$vardas = urlencode($a[0]);               //patvarkom failo pavadinima
			$type = explode("|",$typ);
			if (in_array($ext,$type)) {
				$failai[] = array(                        //talpiname informacija i ARRAY - masyva
				'failas' => $dir.'/'.$failas,
				'laikas' => filemtime($dir.'/'.$failas),
				'dydis' => filesize($dir.'/'.$failas),
				'vardas' => $vardas,
				'tipas' => $ext
				);
			}
		}
		else {
			$a = basename($failas);
			$vardas = urlencode($a);
				$failai[] = array(                        //talpiname informacija i ARRAY - masyva
				'failas' => $dir.'/'.$failas,
				'laikas' => filemtime($dir.'/'.$failas),
				'dydis' => filesize($dir.'/'.$failas),
				'vardas' => $vardas
				);
		}
	}
	$kiek = count($failai);                        //suzinau kiek yra irasu MASYVE $failai
	if (isset($kiek)) {                                   //jeigu masyvas netuscias
		foreach ($failai as $key => $row) {
			$laikas[$key] = $row['laikas'];
		}
		array_multisort($laikas,SORT_DESC,$failai); //surusiuojam failus pagal sukurimo data
		unset($laikas,$kiek,$dir,$key,$row);        //nereikalingus kintamuosius istrinam
		return $failai;
	}
}
unset($text);
//echo blokas("Naujausi failai",$content);
?>